<?php

namespace App\Repositories;

use App\Models\Comment;
use BobCoder\Construct\Foundation\Repository\Repository;

class CommentRepository extends Repository
{
    protected $model;

    public function __construct()
    {
        $this->model = new Comment();
    }


    /**
     * 评论列表
     * @param int $companyId 公司ID
     * @param string $order_sn
     * @param int $driver_id
     * @param int $pageSize
     * @param int $score
     * @return mixed
     * @Author 姿势就是力量
     */
    public function getPageList(int $companyId, string $order_sn,string $user_name,string  $nick_name,string $driver_name,string $company_name,string  $remark,string $date,int $driver_id, int $score,int $user_phone, int $pageSize = 10,string $type)
    {

        return $this->model
            ->where('type',$type)
            ->with(['user:id,name,phone,nick_name', 'driver:id,name', 'company:id,name','orders:id,order_sn'])
            ->when($order_sn, function ($query) use ($order_sn) {
                $query->whereHas('orders', function ($query2) use ($order_sn){
                    $query2->where('order_sn','like','%'.$order_sn.'%'); //订单编号
                });
            })
            ->when($user_name, function ($query) use ($user_name) {
                $query->whereHas('user', function ($query2) use ($user_name){
                    $query2->where('name', 'like','%'.$user_name.'%');    //用户名称
                });
            })
            ->when($nick_name, function ($query) use ($nick_name) {
                $query->whereHas('user', function ($query2) use ($nick_name){
                    $query2->where('nick_name', 'like','%'.$nick_name.'%');  //用户昵称
                });
            })
            ->when($user_phone, function ($query) use ($user_phone) {
                $query->whereHas('user', function ($query2) use ($user_phone){
                    $query2->where('phone', 'like','%'.$user_phone.'%');  //用户手机号
                });
            })
            ->when($driver_name, function ($query) use ($driver_name) {
                $query->whereHas('driver', function ($query2) use ($driver_name){
                    $query2->where('name', 'like','%'.$driver_name.'%');  //司机名称
                });
            })
            ->when($company_name, function ($query) use ($company_name) {
                $query->whereHas('company', function ($query2) use ($company_name){
                    $query2->where('name', 'like','%'.$company_name.'%');  //公司名称
                });
            })
            ->when($driver_id, function ($query) use ($driver_id) {
                $query->where('driver_id', $driver_id);
            })
            ->when($remark, function ($query) use ($remark) {
                $query->where('remark', 'like','%'.$remark.'%');  //备注
            })
            ->when($score, function ($query) use ($score) {
                $query->where('score', 'like','%'.$score.'%');  //分数
            })
            ->when($date,function ($query)use($date){
                if($date){
                    $list = explode(',',$date);
                    $query->whereBetween('created_at',[$list[0],$list[1]]); //时间选择
                }
            })
            ->company($companyId)
            ->paginate($pageSize);
    }


    /**
     *
     * @param array $condition
     * @return mixed
     * @Author 姿势就是力量
     */
    public function scoreAvg(array $condition)
    {
        return $this->model->where($condition)->avg('score') ?? 5;
    }


}
